package com.coooliang.apibook;

import com.coooliang.po.ApiModel;
import com.coooliang.po.DocumentModel;
import com.coooliang.po.ProjectModel;
import com.coooliang.po.TreeModel;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;

import java.util.List;

public class MainController {
    @FXML
    private Scene scene;

    @FXML
    private BorderPane rootPane;

    @FXML
    private BorderPane leftPane;

    @FXML
    private AnchorPane leftTopPane;

    @FXML
    private TabPane centerPane;

    private DocumentModel documentModel;

    @FXML
    private void onShowing() {
        TextField searchField = new TextField();
        searchField.setLayoutX(10);
        searchField.setLayoutY(0);
        searchField.setPrefWidth(190);
        searchField.setPrefHeight(35);

        Button ssButton = new Button("#");
        ssButton.setLayoutX(210);
        ssButton.setLayoutY(0);
        ssButton.setPrefHeight(35);
        ssButton.setPrefWidth(35);

        Button addButton = new Button("+");
        addButton.setLayoutX(255);
        addButton.setLayoutY(0);
        addButton.setPrefHeight(35);
        addButton.setPrefWidth(35);
        addButton.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                ApiTab tab = new ApiTab(new TreeModel());
                centerPane.getTabs().add(tab);
                centerPane.getSelectionModel().selectLast();
            }
        });
        leftTopPane.getChildren().addAll(searchField, ssButton, addButton);

        final TreeItem<TreeModel> root = new TreeItem();
        root.setExpanded(true);
        List<ProjectModel> projectModels = documentModel.getProjects();
        for (int i = 0; i < projectModels.size(); i++) {
            ProjectModel projectModel = projectModels.get(i);
            TreeModel ptModel = new TreeModel(projectModel.getName());
            TreeItem<TreeModel> projectItem = new TreeItem(ptModel);
            if (i == 0) projectItem.setExpanded(true);
            List<ApiModel> apiModels = projectModel.getApis();
            for (ApiModel apiModel : apiModels) {
                TreeModel atModel = new TreeModel(apiModel.getName());
                atModel.setApiModel(apiModel);
                TreeItem apiItem = new TreeItem<TreeModel>(atModel);
                projectItem.getChildren().add(apiItem);
            }
            root.getChildren().add(projectItem);
        }
        TreeTableColumn<TreeModel, String> column = new TreeTableColumn<>("项目列表");
        column.setSortable(false);
        column.setCellValueFactory((TreeTableColumn.CellDataFeatures<TreeModel, String> model) -> new ReadOnlyStringWrapper(model.getValue().getValue().getName()));//树数据
        column.setPrefWidth(leftPane.getPrefWidth());
        TreeTableView<TreeModel> treeTableView = new TreeTableView(root);
        treeTableView.setBorder(Border.EMPTY);
        treeTableView.getColumns().add(column);
        treeTableView.setPrefWidth(leftPane.getPrefWidth());
        treeTableView.setPrefHeight(leftPane.getPrefHeight());
        treeTableView.setShowRoot(false);
        treeTableView.setColumnResizePolicy(TreeTableView.CONSTRAINED_RESIZE_POLICY);//隐藏滚动条
        treeTableView.setBorder(Border.EMPTY);
        treeTableView.setStyle("-fx-border-width: 0;");
        leftPane.setCenter(treeTableView);

        treeTableView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if (event.getClickCount() == 2) {
                    TreeItem<TreeModel> item = treeTableView.getSelectionModel().getSelectedItem();
                    TreeModel model = item.getValue();
                    if (model.isApiItem()) {
                        ApiTab tab = new ApiTab(model);
                        centerPane.getTabs().add(tab);
                        centerPane.getSelectionModel().selectLast();
                    }
                }
            }
        });
    }

    public DocumentModel getDocumentModel() {
        return documentModel;
    }

    public void setDocumentModel(DocumentModel documentModel) {
        this.documentModel = documentModel;
    }


    private class ApiTab extends Tab {
        private TreeModel treeModel;

        public ApiTab(TreeModel treeModel) {
            super();
            this.treeModel = treeModel;
            String apiName = treeModel.getName();
            ApiModel apiModel = treeModel.getApiModel();

            Label titleLabel = new Label(apiName);
            titleLabel.setPrefWidth(110);
            titleLabel.setStyle("-fx-font-size: 14;-fx-font-family: arial;");
            this.setGraphic(titleLabel);

            AnchorPane anchorPane = new AnchorPane();
            TabPane paramTab = new TabPane();
            paramTab.setLayoutX(0);
            paramTab.setLayoutY(0);
            paramTab.prefWidth(730);
            paramTab.prefHeight(100);
            paramTab.getTabs().addAll(new DocTab(0, apiModel), new DocTab(1, apiModel));
            anchorPane.getChildren().add(paramTab);
            this.setContent(anchorPane);
        }

        private class SexModel {
            private String name;

            public SexModel(String name) {
                this.name = name;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
        }

        private class DocTab extends Tab {
            private int index;
            private ApiModel apiModel;
            public DocTab(int index,ApiModel apiModel) {
                this.index = index;
                this.apiModel = apiModel;
                this.setClosable(false);
                if (index == 0){
                    Label titleLabel = new Label("文档");
                    titleLabel.setPrefWidth(110);
                    titleLabel.setStyle("-fx-font-size: 14;-fx-font-family: arial;");
                    this.setGraphic(titleLabel);
                    createDoc();
                }else if(index == 1){
                    Label titleLabel = new Label("修改文档");
                    titleLabel.setPrefWidth(110);
                    titleLabel.setStyle("-fx-font-size: 14;-fx-font-family: arial;");
                    this.setGraphic(titleLabel);
                    createEditDoc();
                }
            }
            private void createEditDoc(){

            }
            private void createDoc(){
                AnchorPane anchorPane = new AnchorPane();
                Label methodLabel = new Label();
                methodLabel.setLayoutX(10);
                methodLabel.setLayoutY(0);
                methodLabel.setPrefWidth(80);
                methodLabel.setPrefHeight(35);
                methodLabel.setText(apiModel.getType());
                methodLabel.setStyle("-fx-background-color: green;");

                Label urlLabel = new Label();
                urlLabel.setLayoutX(methodLabel.getLayoutX()+methodLabel.getPrefWidth());
                urlLabel.setLayoutY(methodLabel.getLayoutY());
                urlLabel.setPrefWidth(150);
                urlLabel.setPrefHeight(methodLabel.getPrefHeight());
                if (apiModel != null) {
                    urlLabel.setText(apiModel.getUrl());
                }
//                urlLabel.prefWidthProperty().bind(Bindings.add(anchorPane.widthProperty(), -150));

                Button saveButton = new Button("保存");
                saveButton.setLayoutX(700);
                saveButton.setLayoutY(urlLabel.getLayoutY());
                saveButton.setPrefWidth(80);
                saveButton.setPrefHeight(30);
//                saveButton.prefWidthProperty().bind(Bindings.add(anchorPane.widthProperty(), -100));

                TabPane paramTab = new TabPane();
                paramTab.setLayoutX(10);
                paramTab.setLayoutY(50);
                paramTab.prefWidth(800);
                paramTab.prefHeight(100);
                paramTab.getTabs().addAll(new ParamTab("param"), new ParamTab("body"), new ParamTab("headers"));

                //add nodes
                anchorPane.getChildren().add(methodLabel);
                anchorPane.getChildren().add(urlLabel);
                anchorPane.getChildren().add(saveButton);
                anchorPane.getChildren().add(paramTab);
                this.setContent(anchorPane);
            }
        }

        private class ParamTab extends Tab {
            private String name;

            public ParamTab(String name) {
                this.name = name;
                this.setClosable(false);
                Label titleLabel = new Label(name);
                titleLabel.setPrefWidth(110);
                titleLabel.setStyle("-fx-font-size: 14;-fx-font-family: arial;");
                this.setGraphic(titleLabel);

                TableView tableView = new TableView();
                tableView.setPrefWidth(730);
                tableView.setPrefHeight(100);
                TableColumn<ApiModel, String> nameCol = new TableColumn("参数");
                TableColumn<ApiModel, String> typeCol = new TableColumn("类型");
                TableColumn<ApiModel, String> descCol = new TableColumn("说明");
                tableView.setEditable(true);
                tableView.getColumns().addAll(nameCol, typeCol, descCol);

                ObservableList<ApiModel> data = FXCollections.observableArrayList(
                        new ApiModel(),
                        new ApiModel(),
                        new ApiModel()
                );
                tableView.setItems(data);
                this.setContent(tableView);
            }
        }
    }
}
